﻿<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="MsAjaxClient._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/act/40412/start.js"></script>


<button id="btnExecute">Execute</button>
<h2>Agencies:</h2>
<div id="results"></div>

<script type="text/javascript">
    var basicUrl = "http://transit.cloudapp.net/TransitOdataService.svc";

    function u(relativeUrl) {
        var url = relativeUrl;
        var hasQueryParameters = relativeUrl.indexOf('?') >= 0;
        url += hasQueryParameters ? '&' : '?';
        url += "api_key=M8xudPElsU2Gwe87MzYKVg";
        return url;
    }

    function doQuery() {
        var odataService;
        Sys.require([Sys.components.dataView, Sys.components.openDataContext, Sys.components.openDataServiceProxy], function () {
            odataService = new Sys.Data.OpenDataServiceProxy(basicUrl);
            odataService.query(u("/Agencies"), cbSuccess, cbFailure);
        });
    }

    function cbFailure(error, context, operation) {
        alert("Callback: failure. \n" + error.get_message());
    }

    function cbSuccess(result, context, operation) {
        var agencies = result;
        for (var i = 0; i < agencies.length; i++) {
            var name = agencies[i].Name;
            $('#results').append('<p>' + name + '</p>');
        }
    }

    Sys.require([Sys.scripts.jQuery], function () {
        $('#btnExecute').click(function (evt) {
            evt.preventDefault();

            doQuery();
        });
    });

</script>

</asp:Content>
